package pt.inescid.components.policyengine.heimdall.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pt.inescid.components.policyengine.heimdall.authorization.AuthorizationAS;
import pt.inescid.components.policyengine.heimdall.base.User;
import pt.inescid.components.policyengine.heimdall.commons.Log;
import pt.inescid.components.policyengine.heimdall.commons.MyServletException;
import pt.inescid.components.policyengine.heimdall.database.UserDAO;

public class EventReceivalFC extends HttpServlet {

	public void doGet(
					HttpServletRequest request,
					HttpServletResponse response)
			throws IOException, ServletException {

		response.setContentType("text/html");
		response.setDateHeader("Expires", 0);
		response.setHeader("Pragma", "no-cache");

		if (request.getProtocol().equals("HTTP/1.1")) {
			response.setHeader("Cache-Control", "no-cache");
		}

/* ===========================================================
==============================================================
=============                    =============================
=============      control access          =============================
=============                    =============================
==============================================================
=========================================================== */
		String userId = request.getParameter("user");
		String userToken = request.getParameter("token");
		
		User currentUser = null;
		if (userId != null && userToken != null && 
				AuthorizationAS.isAccessAllowed(Integer.parseInt(userId), 
						userToken, "EventReceivalFC")) {
			currentUser = UserDAO.getRecord(Long.parseLong(userId));	
		}
		else {
			throw new MyServletException(MyServletException.ACCESS_DENIED);
		}


/* ===========================================================
==============================================================
=============                    =============================
=============      Global variables        ===========
=============                    =============================
==============================================================
=========================================================== */

		String nextURL = null;
		
/* ===========================================================
==============================================================
=============                    =============================
=============      Read Request Parameters        ===========
=============                    =============================
==============================================================
=========================================================== */

		Log.debug(Log.INFO, "Arrived.", "PolicyUploadFC");
/* ===========================================================
==============================================================
=============                    =============================
=============      Begin processing actions        ===========
=============                    =============================
==============================================================
=========================================================== */

		nextURL = "/heimdall/policyUpload.jsp?message=Tudo Ok";

/* ===========================================================
==============================================================
=============                    =============================
=============      FINALLY - Redirect    =====================
=============                    =============================
==============================================================
=========================================================== */

		
		Log.debug(Log.DEBUG, "Invoking " + nextURL, "PolicyUploadFC");
		
		response.encodeURL(nextURL);
		response.sendRedirect(nextURL);

	};



/* ===========================================================
==============================================================
=============                    =============================
=============      doPost       =====================
=============                    =============================
==============================================================
=========================================================== */
	public void doPost(HttpServletRequest request, 
                      HttpServletResponse response)
        throws IOException, ServletException {
       doGet(request, response);
    }
}
